<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">

        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请人</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowuser" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请部门</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowdept" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid&page=stuff_jie','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid&page=stuff_jie','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>结算主题</label>
                        <div class="layui-input-block">
                            <input type="text" id="zhuname" name="zhuname" placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>结算单号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>供应商</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="gonger" class="layui-input gonger" onclick="openPage('../public/choose_dealcom.php?dom=gonger&page=contract_shi','80%','80%')">
                            <div class="choose_div" id="gonger" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_dealcom.php?dom=gonger&page=contract_shi','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">采购合同名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="caiid" class="layui-input caiid" onclick="choose_contract_cai()">
                            <div class="choose_div" id="caiid" key="0"></div>
                            <span class="choose_one" onclick="choose_contract_cai()"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>结算金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="money" id="money" placeholder="请输入" disabled autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>结算日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="jiedt" id="jiedt" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">经手人</label>
                        <div class="layui-input-block">
                            <input type="text" name="baner" id="baner" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">关联单据</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="flowrelas" class="layui-input flowrelas" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');">
                            <div class="choose_div" id="flowrelas" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="advanersDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">预审人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"></div>
                                <div class="tanxiannei" id="advaners"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="nexterDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>指定下一步审核人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"></div>
                                <div class="tanxiannei" id="nexter"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include '../public/upinfo.php'?>
        </div>



        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>结算明细</span></div>
                        <div>
                            <button type="button" onclick="choose_stuff_ru_list_pagejie()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">从入库单导入</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>物资名称及规格</th>
                            <th>单位</th>
                            <th>单价(元)</th>
                            <th>入库数量</th>
                            <th>入库金额(元)</th>
                            <th>入库日期</th>
                            <th>本次结算数量</th>
                            <th>本次结算金额(元)</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="jie_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>


    </div>
</form>
<script>
    $("#commentForm").validate({
        rules: {
            zhuname: {required: true},
            jiedt: {required: true},
        },
        messages:{
            zhuname: {required: "请输入"},
            jiedt: {required: "请选择"},
        }
    });


    function choose_contract_cai(){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目名称");return false;
        }
        var gonger = get_choose_val('gonger');
        if(!gonger){
            facade.error("请先选择供应商");return false;
        }
        openPage('../public/choose_contract_cai.php?dom=caiid&page=stuff_jie&enginid='+enginid+'&gonger='+encodeURI(gonger),'80%','80%')
    }




    function choose_stuff_ru_list_pagejie(obj){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目名称");return false;
        }

        var caiid = get_choose_val('caiid');
        if(!caiid){
            caiid = 0;
        }
        var gonger = get_choose_val('gonger');
        openPage('../public/choose_stuff_ru_list_pagejie.php?page=stuff_jie&enginid='+enginid+'&caiid='+caiid+'&gonger='+encodeURI(gonger),'80%','80%');
    }
    function choose_stuff_ru_list_pagejie_one(obj){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目名称");return false;
        }

        var type = 0;
        var caiid = get_choose_val('caiid');
        if(!caiid){
            caiid = 0;
        }else{
            type = caiid;
        }
        var gonger = get_choose_val('gonger');
        var domkey = $(obj).parents('tr').index();
        openPage('../public/choose_stuff_ru_list_pagejie.php?choose_type=radio&page=stuff_jie_one&enginid='+enginid+'&caiid='+caiid+'&type='+type+'&gonger='+encodeURI(gonger)+'&domkey='+domkey,'80%','80%');
    }


    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_flowdata_add_stuff_jie}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;
                $("#flowsetname").text(data.flowsetname);
                $("#flowuser").val(data.flowuser);
                $("#flowdept").val(data.flowdept);
                $("#number").val(data.number);
                if(data.isadvan){
                    $("#advanersDiv").removeClass('layui-hide');
                }
                if(data.isnext){
                    $("#nexterDiv").removeClass('layui-hide');
                }


                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td>';
                listAddStr += '<input type="text" onclick="choose_stuff_ru_list_pagejie_one(this)" listid="0" readonly class="layui-input name disabled">';
                listAddStr += '</td>';
                listAddStr += '<td><input type="text" disabled class="layui-input unit disabled"></td>';
                listAddStr += '<td><input type="number" disabled class="layui-input price disabled"></td>';
                listAddStr += '<td><input type="number" disabled class="layui-input num disabled"></td>';
                listAddStr += '<td><input type="number" disabled class="layui-input money disabled"></td>';
                listAddStr += '<td><input type="text" disabled class="layui-input dt disabled"></td>';
                listAddStr += '<td><input type="number" disabled class="layui-input jienum disabled"></td>';
                listAddStr += '<td><input type="number" class="layui-input jiemoney"></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 1);



                public_upload_file_load('upload_files');
                render_date();
                render_from();
                initParseMoney();
            }
        });


        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');
            data.field.enginid = get_choose_val('enginid');
            data.field.caiid = get_choose_val('caiid');
            if(data.field.isturn > 0){
                var validRes = $("#commentForm").valid();
                data.field.enginid = get_choose_val('enginid');
                formValid('enginid');
                if(data.field.enginid == undefined || !data.field.enginid){
                    validRes = false;
                }

                data.field.gonger = get_choose_val('gonger');
                formValid('gonger');
                if(data.field.gonger == undefined || !data.field.gonger){
                    validRes = false;
                }

                if(!validRes){return false;}
            }




            var list = [''];
            try {
                $('#jie_list tr').each(function (i) {
                    var listid = $(this).find('.name').attr('listid');
                    var name = $(this).find('.name').val();
                    var unit = $(this).find('.unit').val();
                    var price = $(this).find('.price').val();
                    var num = $(this).find('.num').val();
                    var money = $(this).find('.money').val();
                    var dt = $(this).find('.dt').val();
                    var jienum = $(this).find('.jienum').val();
                    var jiemoney = $(this).find('.jiemoney').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(data.field.isturn > 0){
                        if(!listid){
                            throw new Error('第'+index+'行，请选择物资名称及规格');
                        }

                        var chongKey = check_chongfu(i, listid);
                        if(chongKey){
                            throw new Error('第'+chongKey+'行'+'与第'+index+'行，数据重复');
                        }
                    }



                    list.push({
                        listid:listid,
                        name:name,
                        unit:unit,
                        num:num,
                        price:price,
                        money:money,
                        dt:dt,
                        jienum:jienum,
                        jiemoney:jiemoney,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;



            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);
            public_flow_submit(data.field, ask_flowdata_add_stuff_jie, ask_flowdata_edit_stuff_jie, $(this).attr('but'));

            return false;
        });

    });




    //检测重复
    function check_chongfu(index, listid){
        try {
            $('#chu_list tr').each(function (i) {
                var cur_listid = $(this).find('.name').attr('listid');
                if(index != i){
                    if(listid == cur_listid){
                        throw new Error(i+1);
                    }
                }
            });
        } catch(e) {
            return e.message;
        }

        return 0;
    }


    $('#jie_list').on('input propertychange','.jiemoney',function () {
        jisuan_money_all();
    });
    function jisuan_money_all() {
        var allmoney = 0;
        $('#jie_list tr').each(function (i) {
            var jiemoney = parseValidNum($(this).find('.jiemoney').val());
            allmoney = parseFloat(allmoney) + parseFloat(jiemoney);
        });
        $("#money").val(allmoney);
        initParseMoney();
    }






    function update_refresh_row_func() {
        render_date();
        render_from();
        jisuan_money_all();
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }





    function choose_enginid_delete() {
        $("#caiid").html('');
        $("#gonger").html('');
        $("#jie_list").html('');
    }

    function choose_gonger_delete() {
        $("#caiid").html('');
        $("#jie_list").html('');
    }

    function choose_caiid_delete() {
        $("#jie_list").html('');
    }





</script>
</body>
</html>
